package com.deeper.customview.ui.c6.demo.c06d03;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class CornerWithMarginLayout extends ViewGroup {
    public CornerWithMarginLayout(Context context) {
        super(context);
    }


    public CornerWithMarginLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public CornerWithMarginLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(Color.GRAY);
    }


    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(this.getContext(), attrs);
    }


    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //先测量所有子组件
        this.measureChildren(widthMeasureSpec, heightMeasureSpec);
        //再测量自己大小
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int leftPadding = getPaddingLeft();
        int rightPadding = getPaddingRight();
        int topPadding = getPaddingTop();
        int bottomPadding = getPaddingBottom();

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
            int leftMargin = layoutParams.leftMargin;
            int rightMargin = layoutParams.rightMargin;
            int topMargin = layoutParams.topMargin;
            int bottomMargin = layoutParams.bottomMargin;

            if (i == 0) {
                //定位到左上角
                child.layout(leftPadding + leftMargin,
                        topPadding + topMargin,
                        child.getMeasuredWidth() + leftPadding + leftMargin,
                        child.getMeasuredHeight() + topPadding + topMargin);
            } else if (i == 1) {
                //定位右上角
                child.layout(getMeasuredWidth() - child.getMeasuredWidth() - rightPadding - rightMargin,
                        topPadding + topMargin,
                        getMeasuredWidth() - rightPadding - rightMargin,
                        child.getMeasuredHeight() + topPadding + topMargin);
            } else if (i == 2) {
                //定位左下角
                child.layout(leftPadding + leftMargin,
                        getMeasuredHeight() - child.getMeasuredHeight() - bottomPadding - bottomMargin,
                        child.getMeasuredWidth() + leftPadding + leftMargin,
                        getMeasuredHeight() - bottomPadding - bottomMargin);
            } else if (i == 3) {
                //定位右下角
                child.layout(getMeasuredWidth() - child.getMeasuredWidth() - rightPadding - rightMargin,
                        getMeasuredHeight() - child.getMeasuredHeight() - bottomPadding - bottomMargin,
                        getMeasuredWidth() - rightPadding - rightMargin,
                        getMeasuredHeight() - bottomPadding - bottomMargin);
            }
        }
    }


    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);

        int width = 0;
        if (mode == MeasureSpec.EXACTLY) { //match_parent 或 具体值
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            int aWidth = 0;
            int bWidth = 0;
            int cWidth = 0;
            int dWidth = 0;

            int marginHa = 0;
            int marginHb = 0;
            int marginHc = 0;
            int marginHd = 0;

            for (int i = 0; i < this.getChildCount(); i++) {
                MarginLayoutParams layoutParams = (MarginLayoutParams) getChildAt(i).getLayoutParams();
                if (i == 0) {
                    aWidth = getChildAt(i).getMeasuredWidth();
                    marginHa += layoutParams.leftMargin + layoutParams.rightMargin;
                } else if (i == 1) {
                    bWidth = getChildAt(i).getMeasuredWidth();
                    marginHb += layoutParams.leftMargin + layoutParams.rightMargin;
                } else if (i == 2) {
                    cWidth = getChildAt(i).getMeasuredWidth();
                    marginHc += layoutParams.leftMargin + layoutParams.rightMargin;
                } else if (i == 3) {
                    dWidth = getChildAt(i).getMeasuredWidth();
                    marginHd += layoutParams.leftMargin + layoutParams.rightMargin;
                }
            }
            width = Math.max(aWidth, cWidth) + Math.max(bWidth, dWidth)
                    + getPaddingLeft() + getPaddingRight()
                    + Math.max(marginHa, marginHc)
                    + Math.max(marginHb, marginHd);
        }
        return width;
    }


    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);

        int height = 0;
        if (mode == MeasureSpec.EXACTLY) { //match_parent 或 具体值
            height = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            int aHeight = 0;
            int bHeight = 0;
            int cHeight = 0;
            int dHeight = 0;

            int marginVa = 0;
            int marginVb = 0;
            int marginVc = 0;
            int marginVd = 0;

            for (int i = 0; i < this.getChildCount(); i++) {
                MarginLayoutParams layoutParams = (MarginLayoutParams) getChildAt(i).getLayoutParams();
                if (i == 0) {
                    aHeight = getChildAt(i).getMeasuredHeight();
                    marginVa = layoutParams.topMargin + layoutParams.bottomMargin;
                } else if (i == 1) {
                    bHeight = getChildAt(i).getMeasuredHeight();
                    marginVb = layoutParams.topMargin + layoutParams.bottomMargin;
                } else if (i == 2) {
                    cHeight = getChildAt(i).getMeasuredHeight();
                    marginVc = layoutParams.topMargin + layoutParams.bottomMargin;
                } else if (i == 3) {
                    dHeight = getChildAt(i).getMeasuredHeight();
                    marginVd = layoutParams.topMargin + layoutParams.bottomMargin;
                }
            }
            height = Math.max(aHeight, bHeight) + Math.max(cHeight, dHeight)
                    + getPaddingTop() + getPaddingBottom()
                    + Math.max(marginVa, marginVb)
                    + Math.max(marginVc, marginVd);
        }
        return height;
    }

}